package com.zhijiang.hospital.controller;


import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.zhijiang.common.util.ApiResult;
import com.zhijiang.common.util.NucleicQuery;
import com.zhijiang.hospital.entrty.dto.LisTestRegDto;
import com.zhijiang.hospital.entrty.dto.NucleicCount;
import com.zhijiang.hospital.service.NucleicService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @author 75936
 * @version 1.0
 * @project demo_hesuan
 * @description
 * @date 2022-12-14 08:52:02
 */
@RestController
@RequestMapping("Nucleic")
public class    NucleicController {
    @Resource
    private NucleicService nucleicService;

    @PostMapping("data")
    public ApiResult findNucleicData(@RequestBody NucleicQuery nucleicQuery){
        return ApiResult.ok(nucleicService.findPeopleNucleicCount(nucleicQuery));
    }

    @PostMapping("findPeopleNucleicData")
    public ApiResult findPeopleNucleicData(@RequestBody NucleicQuery nucleicQuery){


        return ApiResult.ok(nucleicService.findPeopleNucleicData(nucleicQuery));
    }

    @PostMapping("loadExcel")
    public void loadExcel(HttpServletResponse response,@RequestBody NucleicQuery nucleicQuery) throws IOException {
       // List<LisTestRegDto> list = nucleicService.findPeopleNucleicData(nucleicQuery);


        List<LisTestRegDto>  peopleNucleicData = nucleicService.findPeopleNucleicData(nucleicQuery);
//        final int idlsize = peopleNucleicData.size();
//        for (int i = 0; i < idlsize; i+= 1000000) {
//            List<LisTestRegDto> lisTestRegDtos = peopleNucleicData.subList(i, i + 1000000 > idlsize ? idlsize : i + a);
//            System.out.println(lisTestRegDtos+""+new Date());
//        }

        String title = "核酸结果";
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + RandomUtil.randomNumbers(4) + URLEncoder.encode("核酸结果.xlsx","utf-8"));
        //4. 创建输入、输出流
        EasyExcel.write(response.getOutputStream(), LisTestRegDto.class).sheet(title).doWrite(peopleNucleicData);
    }

    @PostMapping("count")
    private ApiResult count(@RequestBody NucleicQuery nucleicQuery){
            NucleicCount count =  nucleicService.findCount(nucleicQuery);
        return ApiResult.ok(count);
    }


}
